package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.service.IStrategyThemeService;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 攻略文章管理控制器
 */

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    //主题
    @Reference
    private IStrategyThemeService strategyThemeService;

    //分类
    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyTagService strategyTagService;

    /**
     * 攻略文章管理的分页
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model) {
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    /**
     * 攻略文章管理的新增操作(查内容和分类)
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            //查询文章
            StrategyDetail detail = strategyDetailService.get(id);
            //查询文章内容
            StrategyContent content = strategyDetailService.getContent(id);
            //把内容设置给文章
            detail.setStrategyContent(content);
            model.addAttribute("entity", detail);
        }

        //分类
        model.addAttribute("catalogs", strategyCatalogService.list());

        //主题
        model.addAttribute("themes", strategyThemeService.list());

        //回显标签
        model.addAttribute("tags", strategyTagService.getByDetilId(id));


        return "strategyDetail/input";

    }

    /**
     * 攻略文章管理的新增操作
     *
     * @param entity
     * @param tags
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail entity, String tags) {
        AjaxResult result = new AjaxResult();
        try {
            strategyDetailService.saveOrUpdate(entity, tags);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }


}